<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-07-18
 * Time: 下午 4:10
 * 预约控制器
 */
namespace app\admin\controller;

use app\admin\model\AppModel;

class App extends Base
{
    /**
     * 预约列表
     */
    public function index(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['app_phone'] = ['like', '%' . $param['searchText'] . '%'];
            }
            if (!empty($param['status'])) {
                $where['app_status'] = $param['status'];
            }
            $app = new AppModel();
            $selectResult = $app->getAppByWhere($where, $offset, $limit);

            foreach($selectResult as $key=>$vo){
                if(!$vo['app_remarks']){
                    $selectResult[$key]['app_remarks']= "<textarea id='remarks_".$vo['app_id']."' onblur='appRemarks(".$vo['app_id'].")'></textarea>";
                }
                $str = '';

                $str .='<select class="form-control" id="status_'.$vo['app_id'].'" onchange="appStatus('.$vo['app_id'].')">';
                if($selectResult[$key]['app_status'] == 1){
                    $str.='<option value="0">未处理</option><option value="1" selected>洽谈成功</option><option value="2">洽谈失败</option>';
                }elseif($selectResult[$key]['app_status'] == 2){
                    $str.='<option value="0">未处理</option><option value="1" >洽谈成功</option><option value="2" selected>洽谈失败</option>';
                }else{
                    $str.='<option value="0" selected>未处理</option><option value="1" >洽谈成功</option><option value="2" >洽谈失败</option>';
                }
                $str.='</select>';

                $selectResult[$key]['app_status'] = $str;
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['app_id']));
            }

            $return['total'] = $app->getAllApp($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch('index');
    }

    /**
     * 修改预约备注
     */
    public function appRemarks(){
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'修改失败');
        }
        if(!$param['remarks']){
            ajaxReturn(1,'修改成功');
        }
        $map['app_id'] = $param['id'];
        $data['app_remarks'] = $param['remarks'];
        $app = new AppModel();
        $res = $app->getAppUpdate($map,$data);
        if($res === false){
            ajaxReturn(-1,'修改失败');
        }else{
            ajaxReturn(1,'修改成功');
        }
    }
    /**
     * 修改预约状态
     */
    public function appStatus(){
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'修改失败');
        }
        $map['app_id'] = $param['id'];
        $data['app_status'] = $param['status'];
        $app = new AppModel();
        $res = $app->getAppUpdate($map,$data);
        if($res === false){
            ajaxReturn(-1,'修改失败');
        }else{
            ajaxReturn(1,'修改成功');
        }
    }
    /**
     * 删除预约记录
     */
    public function appDel(){
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'修改失败');
        }
        $map['app_id'] = $param['id'];
        $app = new AppModel();
        $res = $app->getAppDel($map);
        if($res){
            ajaxReturn(1,'删除成功');
        }else{
            ajaxReturn(-1,'删除失败');
        }
    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'app/appdel',
                'href' => "javascript:appDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}